import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

class ChooseDateWidget extends StatelessWidget {
  final BuildContext context;
  final String title;
  String startTime;
  String endTime;
  String value;
  final bool isDelete;
  final bool isShowHour;
  /// 是否是编辑状态
  final bool isEdit;
  final double? titleWidth;
  /// 是否展示添加或删除a
  final bool isSHowOperateButton;
  final bool isRequired;
  final Function(String startDateTime, String endDateTime) timeChooseCallBack;
  final Function() btnClickCallBack;
  ChooseDateWidget(this.context, this.title, this.startTime, this.endTime,
      this.timeChooseCallBack, this.btnClickCallBack,
      {this.isDelete = false,
        this.isShowHour = false,
        this.isRequired = false,
        this.value = '',
        this.titleWidth,
        this.isSHowOperateButton = true,
        this.isEdit = false,
        Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Log.d('endTime==' + endTime.toString());

    return Container(
      color: Colors.white,
      height: 55,
      alignment: Alignment.center,
      child: Row(
        children: [
          isRequired
              ? Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(
              left: 6.w,
              right: 2.w,
            ),
            child: Text(
              '*',
              style: ytTextStyle(
                color: Colour.warningTextColor,
                fontWeight: FontWeight.w500,
              ),
            ),
          )
              : Gaps.hGap16,
          Container(
            width: titleWidth ?? 90.w,
            color: Colors.white,
            child: Text(
              title,
              style: ytTextStyle(
                  fontWeight: FontWeight.normal,
                  color: Colour.labelTextColor,
                  fontSize: 14),
            ),
          ),
          // Spacer(),
          if (isEdit)GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                _showDatePicker();
              },
              child: Container(
                decoration: const BoxDecoration(
                    color: Colour.backgroundColor,
                    borderRadius: BorderRadius.all(Radius.circular(4))),
                height: 32,
                width: 106.w,
                alignment: Alignment.center,
                child: Text(
                  startTime,
                  style:
                  ytTextStyle(fontWeight: FontWeight.normal, fontSize: 14),
                ),
              )),
          if (isEdit)Container(
            margin: const EdgeInsets.only(left: 3, right: 3),
            alignment: Alignment.center,
            child: Text(
              '-',
              style: ytTextStyle(fontWeight: FontWeight.normal, fontSize: 14),
            ),
          ),
          if (isEdit)GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                _showDatePicker();
              },
              child: Container(
                decoration: const BoxDecoration(
                    color: Colour.backgroundColor,
                    borderRadius: BorderRadius.all(Radius.circular(4))),
                height: 32,
                width: 106.w,
                alignment: Alignment.center,
                child: Text(
                  endTime,
                  style:
                  ytTextStyle(fontWeight: FontWeight.normal, fontSize: 14),
                ),
              )),
          if (isSHowOperateButton && isEdit)
            GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  btnClickCallBack();
                },
                child: Container(
                  padding: EdgeInsets.only(left: 16.w, right: 0),
                  child: Image.asset(
                    isDelete
                        ? 'assets/images/site_elec_price_delete.png'
                        : 'assets/images/site_elec_price_add.png',
                    height: 18,
                    width: 18,
                  ),
                )),
          if (!isEdit)Container(
            margin: EdgeInsets.only(right: 16.w),
            child: Text(value,style: ytTextStyle(fontSize: 14),),
          )
        ],
      ),
    );
  }

  _showDatePicker() {
    String format = 'MM月-dd';
    BrnDateTimeRangePickerMode pickerMode = BrnDateTimeRangePickerMode.date;
    if (isShowHour) {
      format = 'HH:mm';
      pickerMode = BrnDateTimeRangePickerMode.time;
    }
    BrnDateRangePicker.showDatePicker(
      context,
      pickerMode: pickerMode,
      minuteDivider: 1,
      pickerTitleConfig: BrnPickerTitleConfig(
        titleContent: title,
        confirm: Text(
          ResKey.sure.tr,
          style: TextStyle(
            color: Colour.primaryColor,
            fontSize: 16.sp,
          ),
        ),
      ),
      initialStartDateTime: isShowHour
          ? handleHourToDateTime(startTime)
          : handleMonthToDateTime(startTime),
      initialEndDateTime: isShowHour
          ? handleHourToDateTime(endTime)
          : handleMonthToDateTime(endTime),
      dateFormat: format,
      onConfirm:
          (DateTime startDateTime, DateTime endDateTime, startlist, endlist) {
        String startTime = '';
        String sendTime = '';
        if (isShowHour) {
          int startHour = startDateTime.hour;
          int endHour = endDateTime.hour;
          int startMinute = startDateTime.minute;
          int endMinute = endDateTime.minute;
          if (startHour < 10) {}
          // startTime = '${startDateTime.hour.toRadixString(2)}:${startDateTime.minute.toStringAsPrecision(2)}';
          startTime =
          '${handleTime(startDateTime.hour)}:${handleTime(startDateTime.minute)}';
          sendTime =
          '${handleTime(endDateTime.hour)}:${handleTime(endDateTime.minute)}';
        } else {
          startTime =
          '${handleTime(startDateTime.month)}-${handleTime(startDateTime.day)}';
          sendTime =
          '${handleTime(endDateTime.month)}-${handleTime(endDateTime.day)}';
        }

        timeChooseCallBack(startTime, sendTime);
      },
    );
  }

  String handleTime(int time) {
    if (time < 10) {
      return '0$time';
    }
    return time.toString();
  }

  DateTime handleHourToDateTime(String time) {
    if (time == '24:00') {
      time = '23:59';
    }
    List<String> list = time.split(':');
    if (list.isNotEmpty && list.length >= 2) {
      DateTime date =
      DateTime(2023, 1, 1, int.parse(list[0]), int.parse(list[1]));
      return date;
    } else {
      return DateTime.now();
    }
  }
}

DateTime handleMonthToDateTime(String time) {
  List<String> list = time.split('-');
  if (list.isNotEmpty && list.length >= 2) {
    DateTime date = DateTime(2023, int.parse(list[0]), int.parse(list[1]));
    return date;
  } else {
    return DateTime.now();
  }
}
